/**
 *作者：Wanghong
 *
 */
console.clear();

const loginBtn = document.getElementById('login');
const signupBtn = document.getElementById('signup');

const userRegister = document.getElementById('userRegister');
const userLogin = document.getElementById('userLogin');

loginBtn.addEventListener('click', (e) => {
    $(".msg").empty();
    let parent = e.target.parentNode.parentNode;
    Array.from(e.target.parentNode.parentNode.classList).find((element) => {
        if(element !== "slide-up") {
            parent.classList.add('slide-up')
        }else{
            signupBtn.parentNode.classList.add('slide-up')
            parent.classList.remove('slide-up')
        }
    });
});

signupBtn.addEventListener('click', (e) => {
    $(".msg").empty();
    let parent = e.target.parentNode;
    Array.from(e.target.parentNode.classList).find((element) => {
        if(element !== "slide-up") {
            parent.classList.add('slide-up')
        }else{
            loginBtn.parentNode.parentNode.classList.add('slide-up')
            parent.classList.remove('slide-up')
        }
    });
});

userRegister.addEventListener('click',()=>{
    let url = "/amUser/register";
    let date=$("#registerForm").serialize();
    $.post(url,date,function (res) {
        console.info(res);
        if (res.resp==1){
            console.info("成功");
            loginBtn.click();
        }else if (res.resp==2){
            $(".msg").html(res.msg);
        }else {
            $(".msg").html(res.msg);
        }
    },"json");
});

userLogin.addEventListener('click',()=>{
    let url = "/amUser/userLogin";
    let date=$("#loginForm").serialize();
    $.post(url,date,function (res) {
        console.info(res);
        if (res.resp==1){
            sessionStorage.setItem("user",JSON.stringify(res.user));
            location.href="/pages/index.html";
        }else {
            $(".msg").html(res.msg);
        }
    },"json");
});